﻿using Project.Domain.Events;
using Project.Domain.Seedwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.Domain.AggregateModels
{
    public class Project
        : Entity, IAggregateRoot
    {
        public Project()
        {
            Properties = new HashSet<ProjectProperty>();
            Viewers = new HashSet<ProjectViewer>();
            Contributors = new HashSet<ProjectContributor>();
            BPFiles = new HashSet<BPFile>();
        }
        public string Name { get; set; }
        public string Avatar { get; set; }
        public DateTime CreateTime { get; set; }
        public int ProvinceId { get; set; }
        public string ProvinceName { get; set; }
        public int CityId { get; set; }
        public string CityName { get; set; }
        public int AreaId { get; set; }
        public string AreaName { get; set; }
        public DateTime RegisteTime { get; set; }
        public virtual ICollection<ProjectProperty> Properties { get; set; }
        public virtual ICollection<ProjectViewer> Viewers { get; set; }
        public virtual ICollection<ProjectContributor> Contributors { get; set; }
        public virtual ICollection<BPFile> BPFiles { get; set; }
        public virtual ProjectVisibleRule ProjectVisibleRule { get; set; }
        public int UserId { get; set; }
        public string Introduction { get; set; }

        public Project CloneProject(Project source = null)
        {
            if (source == null)
            {
                source = this;
            }

            var newProject = new Project()
            {
                Name = source.Name,
                Avatar = source.Avatar,
                CreateTime = source.CreateTime,
                RegisteTime = source.RegisteTime,
                ProvinceId = source.ProvinceId,
                ProvinceName = source.ProvinceName,
                CityId = source.CityId,
                CityName = source.CityName,
                AreaId = source.AreaId,
                AreaName = source.AreaName,
            };
            foreach (var item in source.Properties)
            {
                newProject.Properties.Add(new ProjectProperty()
                {
                    Key = item.Key,
                    Text = item.Text,
                    Value = item.Value
                });
            }
            return newProject;
        }

        public void AddViewer(ProjectViewer projectViewer)
        {
            if (!Viewers.Any(v => v.Id == projectViewer.Id))
            {
                Viewers.Add(projectViewer);
                AddDomainEvent(new ProjectViewedEvent() { ProjectViewer=projectViewer });
            }
        }
        public void AddContributor(ProjectContributor projectContributor)
        {
            if (!Contributors.Any(v => v.Id == projectContributor.Id))
            {
                Contributors.Add(projectContributor);
                AddDomainEvent(new ProjectJoinedEvent() {  ProjectContributor = projectContributor });
            }
        }
    }
}
